package com.koala.learn.threadlearn.test2;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;

/**
 * @Description TODO
 * @Date 2022/3/22 20:16
 * @Created by koala
 */
@AllArgsConstructor
@Data
@Slf4j
public class Kitchen implements Runnable {

    // 限制厨房的厨师数量
    private Semaphore semaphore;

    @SneakyThrows
    private void add() {
        semaphore.acquire();
        int space = semaphore.availablePermits();
        log.info("厨师{}进入厨房 ", Thread.currentThread().getName());
        Thread.sleep(1000);
        log.info("厨师{}走出厨房", Thread.currentThread().getName());
        semaphore.release();
    }

    @Override
    public void run() {
        add();
    }
}
